package gold.digger;

import gold.utils.InputUtil;

import java.util.*;
import java.util.List;

/**
 * Created by fanzhenyu02 on 2020/6/27.
 * common problem solver template.
 */
public class LC318 {
    public long startExecuteTime = System.currentTimeMillis();


    class Solution {
        public int maxProduct(String[] words) {
            Map<Integer, Integer> map = new HashMap<Integer, Integer>();

            for (String word : words) {
                int key = getStrKey(word);
                int value = map.getOrDefault(key, 0) > word.length() ? map.getOrDefault(key, 0) : word.length();
                map.put(key, value);
            }

            //这里将map.entrySet()转换成list
            List<Map.Entry<Integer, Integer>> list = new ArrayList<Map.Entry<Integer, Integer>>(map.entrySet());
            //然后通过比较器来实现排序
            Collections.sort(list, new Comparator<Map.Entry<Integer, Integer>>() {
                //降序排序
                @Override
                public int compare(Map.Entry<Integer, Integer> o1,
                                   Map.Entry<Integer, Integer> o2) {
                    return o2.getValue() - o1.getValue();
                }
            });

            int maxProduct = 0;
            for (int i = 0; i < list.size(); i++) {
                Map.Entry<Integer, Integer> cur = list.get(i);
                for (int j = i + 1; j < list.size(); j++) {
                    Map.Entry<Integer, Integer> next = list.get(j);
                    if (isOverLap(cur.getKey(), next.getKey())) continue;
                    maxProduct = Math.max(maxProduct, cur.getValue() * next.getValue());
                    break;
                }
            }

            return maxProduct;
        }

        public boolean isOverLap(int key1, int key2) {
            return 0 != (key1 & key2);
        }

        public int getStrKey(String word) {
            int key = 0;
            for (char ch : word.toCharArray()) {
                int curOnePos = ch - 'a';
                key |= (1 << curOnePos);
            }

            return key;
        }
    }

    public void run() {
        Solution solution = new Solution();
        String[] arr = InputUtil.toStringArray("[\"a\",\"aa\",\"aaa\",\"aaaa\"]");
//        System.out.println(solution.maxProduct(arr));
        System.out.println(solution.isOverLap(solution.getStrKey("ab"), solution.getStrKey("ac")));
    }

    public static void main(String[] args) throws Exception {
        LC318 an = new LC318();
        an.run();

        System.out.println("\ncurrent solution total execute time: " + (System.currentTimeMillis() - an.startExecuteTime) + " ms.");
    }
}
